package com.ogawa.project3209e.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.project3209e.R
import com.ogawa.project3209e.base.Base3209EMqttActivity
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.project3209e.bean.Massage3209EArmchair
import kotlinx.android.synthetic.main.activity_device_3209e_auto.*

@Route(path = UrlPathConstant.ACTIVITY_3209E_AUTO)
class Device3209eAutoActivity : Base3209EMqttActivity(), View.OnClickListener {
//    private lateinit var tv_dashi: TextView
//    private lateinit var tv_taishi: TextView
//    private lateinit var tv_guangjie: TextView
//    private lateinit var tv_shujin: TextView
//    private lateinit var tv_qingchen: TextView
//    private lateinit var tv_huoli: TextView
//    private lateinit var tv_taiji: TextView
//    private lateinit var tv_nvwang: TextView
//    private lateinit var tv_nanshi: TextView
//    private lateinit var tv_laonian: TextView
//    private lateinit var tv_japanese: TextView
//    private lateinit var tv_indian: TextView
//    private lateinit var tv_paris: TextView
//    private lateinit var tv_chinese: TextView
//    private lateinit var tv_bangong: TextView
//    private lateinit var tv_ditou: TextView
//    private lateinit var tv_jiache: TextView
//    private lateinit var tv_gaogen: TextView
    private var tvCurrent: TextView? = null
    private var program: String = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_device_3209e_auto
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.auto_massage))
        tv_dashi.setOnClickListener(this)
        tv_taishi.setOnClickListener(this)
        tv_guangjie.setOnClickListener(this)
        tv_shujin.setOnClickListener(this)
        tv_qingchen.setOnClickListener(this)
        tv_huoli.setOnClickListener(this)
        tv_taiji.setOnClickListener(this)
        tv_nvwang.setOnClickListener(this)
        tv_nanshi.setOnClickListener(this)
        tv_laonian.setOnClickListener(this)
        tv_japanese.setOnClickListener(this)
        tv_indian.setOnClickListener(this)
        tv_paris.setOnClickListener(this)
        tv_chinese.setOnClickListener(this)
        tv_bangong.setOnClickListener(this)
        tv_ditou.setOnClickListener(this)
        tv_jiache.setOnClickListener(this)
        tv_gaogen.setOnClickListener(this)
        findViewById<TextView>(R.id.tv_cancel).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_confirm).setOnClickListener(this)
        setProgram()
    }

    private fun setProgram() {
        val deviceStatusBean = Massage3209EArmchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val auto = Integer.parseInt(deviceStatusBean.functions.autoProgram.modelValue)
            tv_dashi.isSelected = auto == 1
            tv_taishi.isSelected = auto == 2
            tv_guangjie.isSelected = auto == 3
            tv_shujin.isSelected = auto == 4
            tv_qingchen.isSelected = auto == 5
            tv_huoli.isSelected = auto == 6
            tv_taiji.isSelected = auto == 7
            tv_nvwang.isSelected = auto == 8
            tv_nanshi.isSelected = auto == 9
            tv_laonian.isSelected = auto == 10
            tv_japanese.isSelected = auto == 11
            tv_indian.isSelected = auto == 12
            tv_paris.isSelected = auto == 13
            tv_chinese.isSelected = auto == 14
            tv_bangong.isSelected = auto == 15
            tv_ditou.isSelected = auto == 16
            tv_jiache.isSelected = auto == 17
            tv_gaogen.isSelected = auto == 18
            when(auto){
                1->tvCurrent=tv_dashi
                2->tvCurrent=tv_taishi
                3->tvCurrent=tv_guangjie
                4->tvCurrent=tv_shujin
                5->tvCurrent=tv_qingchen
                6->tvCurrent=tv_huoli
                7->tvCurrent=tv_taiji
                8->tvCurrent=tv_nvwang
                9->tvCurrent=tv_nanshi
                10->tvCurrent=tv_laonian
                11->tvCurrent=tv_japanese
                12->tvCurrent=tv_indian
                13->tvCurrent=tv_paris
                14->tvCurrent=tv_chinese
                15->tvCurrent=tv_bangong
                16->tvCurrent=tv_ditou
                17->tvCurrent=tv_jiache
                18->tvCurrent=tv_gaogen
            }
        }
    }

    override fun onClick(v: View?) {
        if (v != tvCurrent) {
            v?.isSelected = true
            tvCurrent?.isSelected = false
            tvCurrent = v as TextView?
        }
        when (v?.id) {
            R.id.tv_dashi -> program = "1"
            R.id.tv_taishi -> program = "2"
            R.id.tv_guangjie -> program = "3"
            R.id.tv_shujin -> program = "4"
            R.id.tv_qingchen -> program = "5"
            R.id.tv_huoli -> program = "6"
            R.id.tv_taiji -> program = "7"
            R.id.tv_nvwang -> program = "8"
            R.id.tv_nanshi -> program = "9"
            R.id.tv_laonian -> program = "10"
            R.id.tv_japanese -> program = "11"
            R.id.tv_indian -> program = "12"
            R.id.tv_paris -> program = "13"
            R.id.tv_chinese -> program = "14"
            R.id.tv_bangong -> program = "15"
            R.id.tv_ditou -> program = "16"
            R.id.tv_jiache -> program = "17"
            R.id.tv_gaogen -> program = "18"
            R.id.tv_cancel -> {
//                program = ""
                finish()
            }
            R.id.tv_confirm -> {
                if (program.isNotEmpty()) {
                    setResult(Activity.RESULT_OK, Intent().putExtra("program", program))
                }
                finish()
            }
        }
    }
}